package com.property.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.property.entity.User;

public class mvcController implements HandlerInterceptor{

	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		User user = (User)request.getSession().getAttribute("user");
		if(user==null){
			response.sendRedirect("/property/login");
			return false;
		}
		String pathString=request.getRequestURI();
		int index1=pathString.indexOf("/",pathString.indexOf("/")+1)+1;
		int index2=pathString.indexOf("/",index1);
		pathString=pathString.substring(index1,index2);
		
		if(request.getSession().getAttribute("url")==null){
			request.getSession().setAttribute("url", pathString);
		}
		System.out.println(pathString+"****");
		if("owner".equals(pathString)){
			if(user.getRole().getId()!=1){
				System.out.println("000");
				request.getSession().setAttribute("error", "权限不一致，请重新登陆！");
				//request.getSession().setAttribute("url", s);
				response.sendRedirect("/property/2041");
				return false;
			}
		}
		if("main".equals(pathString)){
			if(user.getRole().getId()!=2){
				System.out.println("999");
				request.getSession().setAttribute("error", "权限不一致，请重新登陆！");
				//request.getSession().setAttribute("url", s);
				response.sendRedirect("/property/2041");
				return false;
			}
		}
		if("admin".equals(pathString)){
			if(user.getRole().getId()!=3){
				System.out.println("111");
				request.getSession().setAttribute("error", "权限不一致，请重新登陆！");
				//request.getSession().setAttribute("url", s);
				response.sendRedirect("/property/2041");
				return false;
			}
		}
		
		return true;
	}

	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("po");
		
	}

	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("af");
		
	}

}
